<?php
namespace app\index\controller;

use think\Controller;
use think\View;

class Index extends controller
{
    public function index()
    {   
    	$view = new View();
    	$battle_id = input('battle_id');
        if(!$battle_id){
            $this->redirect('index/Index/login');
        } 
       
    	$battle_info = db('Wxapp_battle')->where('id',$battle_id)->find();

    	
    	$player1_score[0] = $battle_info['player1_score'] < 10 ? 0 : str_split($battle_info['player1_score'])[0]; 
     
    	$player1_score[1] = $battle_info['player1_score'] < 10 ? $battle_info['player1_score']   : str_split($battle_info['player1_score'])[1];
        $player2_score[0] = $battle_info['player2_score'] < 10 ? 0 : str_split($battle_info['player2_score'])[0]; 
    	$player2_score[1] = $battle_info['player2_score'] < 10 ? $battle_info['player2_score']   : str_split($battle_info['player2_score'])[1];
    	//获取赛事名称
    	$battle_info['event_name'] = db('Event')->where('id',$battle_info['event_id'])->value('event_name');
        
        //处理双打的问题
        $battle_info['player1_name'] = strpos($battle_info['player1_name'],',') ? explode(',',$battle_info['player1_name']) : (strpos($battle_info['player1_name'],'，') ? explode('，',$battle_info['player1_name']) : $battle_info['player1_name']);
        $battle_info['player2_name'] = strpos($battle_info['player2_name'],',') ? explode(',',$battle_info['player2_name']) : (strpos($battle_info['player2_name'],'，') ? explode('，',$battle_info['player2_name']) : $battle_info['player2_name']);
       
        $view->assign('battle_id',$battle_id);
    	$view->assign('battle_info',$battle_info);
    	$view->assign('player1_score',$player1_score);
    	$view->assign('player2_score',$player2_score);

        if($battle_info['battle_type'] !=2){
            return $view->fetch('index');
        }else{
            $view->assign('snooker_score',json_decode($battle_info['snooker_score'],true));
            return $view->fetch('snooker_index');
        } 
    	
       
    }

    //计分板扫码登陆
    public function login(){
    	$view = new View();
        $view->login_code = getRandCode(10);
        $view->client_ip  = getIP();
        return $view->fetch();
    }

      //监听扫码
    public function monitorDoScanCode(){
    	set_time_limit(0);

    	$login_code   = input('login_code');
    	$time         = time();
    	$Wxapp_battle = db('Wxapp_battle');
        while (true) {
        	$result = $Wxapp_battle->where('login_code',$login_code)->find();
           
        	if($result && (time()-$time<30)){
                $Wxapp_battle->where('id',$result['id'])->update(array('status'=>1));
        		$data['state']     = 1;
        		$data['battle_id'] = $result['id'];
        		$data['info']      = '连接成功';
                
        		break;
        	}else if(time()-$time > 30){
        		$data['state']     = 0;
        		$data['info']      = '等待超时,请重新发起请求';
        		break;
        	}
          	sleep(1);
        }  

        return json($data);
    }

     //更新计分板比分
    public function updateScoreBoard(){
    	set_time_limit(0);
    	
    	$battle_id   = input('battle_id');
        $update_time = input('update_time');
    	
    	$time       = time();
        
        $Battle    = db('Wxapp_battle');
        while (true) {
        	$battle_info = $Battle->where('id',$battle_id)->find();
            if($battle_info['status'] != 0){
                if(!$battle_info['next_id']){
                    if($battle_info['update_time'] != $update_time && (time()-$time<25)){
                        $data['state']         = 1;
                        $data['battle_info']   = $battle_info;
                        if($battle_info['battle_type'] == 2){
                            $data['snooker_score'] = json_decode($battle_info['snooker_score']);
                        }
                        break;
                    }else if(time()-$time > 25){
                        $data['state']     = 0;
                        $data['info']      = '请重新发起请求';
                        break;
                    }
                }else{
                    $data['state']         = 2;
                    $data['battle_id']   = $battle_info['next_id'];
                    break;
                }
            }else{
                $data['state'] = 3;
                $data['info']  = '请重新发起请求';
                break;
            }
       
          	sleep(1);
        }  

        return json($data);
    }

}
